﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Models;
using BLL;
using MySql.Data.MySqlClient;

namespace LinSM
{
    public partial class AllItem : Form
    {
        public AllItem()
        {
            InitializeComponent();
            dgvItemList.AutoGenerateColumns = false;//取消dgv自动增加列
        }

        AllItemManager aim = new AllItemManager();
        CharManager cm = new CharManager();
        CharInfoManager cim = new CharInfoManager();
        int itemType=1;

        private void Selected()
        {
            if (dgvItemList.SelectedRows.Count != 0)
            {
                dgvItemList.CurrentRow.Selected = false;//不让这个控件有默认选中
            }
        }

        private void AllItem_Load(object sender, EventArgs e)
        {
            if (Config.verType != 2)
            {
                gethp.Visible = false;
                getmp.Visible = false;
            }
            dgvItemList.DataSource = aim.GetAllItem(itemType);
            Selected();
            tscboSearch.Text = "名称";
            
        }

        private void tsbtnDown_Click(object sender, EventArgs e)
        {
            if (cm.GetAllChar().Table.Rows.Count < cm.PageSize())//判断是否是最后一页
            {
                MessageBox.Show("已经是最后一页");
                return;
            }
            cm.Page(1);//下一页
            dgvItemList.DataSource = aim.GetAllItem(itemType);
            Selected();
        }

        private void tsbtnUp_Click(object sender, EventArgs e)
        {
            cm.Page(0);//上一页
            dgvItemList.DataSource = aim.GetAllItem(itemType);
            Selected();
        }

        private void tsbtnItem1_Click(object sender, EventArgs e)
        {
            Text = "道具列表-背包";
            Column9.Visible = true;//角色隐藏
            Column10.Visible = false;//账号显示
            Column11.Visible = false;//血盟隐藏
            itemType = 1;
            dgvItemList.DataSource = aim.GetAllItem(itemType);
            Selected();
        }

        private void tsbtnItem2_Click(object sender, EventArgs e)
        {
            Text = "道具列表-仓库";
            Column9.Visible = false;//角色隐藏
            Column10.Visible = true;//账号显示
            Column11.Visible = false;//血盟隐藏
            itemType = 2;
            dgvItemList.DataSource = aim.GetAllItem(itemType);
            Selected();
        }

        private void tsbtnItem3_Click(object sender, EventArgs e)
        {
            Text = "道具列表-妖仓";
            Column9.Visible = true;//角色隐藏
            Column10.Visible = false;//账号显示
            Column11.Visible = false;//血盟隐藏
            itemType = 3;
            dgvItemList.DataSource = aim.GetAllItem(itemType);
            Selected();
        }

        private void tsbtnItem4_Click(object sender, EventArgs e)
        {
            Text = "道具列表-盟库";
            Column9.Visible = false;//角色隐藏
            Column10.Visible = false;//账号显示
            Column11.Visible = true;//血盟隐藏
            itemType = 4;
            dgvItemList.DataSource = aim.GetAllItem(itemType);
            Selected();
        }

        private void tsbtnDelete_Click(object sender, EventArgs e)
        {
            if (dgvItemList.SelectedRows.Count == 0)
            {
                return;
            }
            string itemId = dgvItemList.CurrentRow.Cells[0].Value.ToString();
            if (cim.DeleteItem(itemId, itemType))
            {
                dgvItemList.DataSource = aim.GetAllItem(itemType);
                MessageBox.Show("删除成功");
            }
        }

        private bool isInt(string id)
        {
            int x = 0;
            try
            {
                Convert.ToInt32(id);
                x = 1;
            }
            catch (Exception)
            {
            }
            if (x == 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// k=键=列名
        /// v=值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsbtnSearch_Click(object sender, EventArgs e)
        {
            string v = tstxtSearch.Text.Trim();
            if (v == "")
            {
                MessageBox.Show("请输入关键字！");
                tstxtSearch.Select();
                return;
            }
            if (tscboSearch.SelectedIndex != 0)
            {
                if (isInt(v))
                {
                    MessageBox.Show("关键字格式错误！");
                    tstxtSearch.Select();
                    return;
                }
            }
            string k = tscboSearch.SelectedItem.ToString();
            switch (k)
            {
                case "编号":
                    dgvItemList.DataSource = aim.Search(k, v, 0, itemType);
                    Selected();
                    break;
                case "名称":
                    dgvItemList.DataSource = aim.Search("item_name", v, 2, itemType);
                    Selected();
                    break;
                default:
                    dgvItemList.DataSource = aim.Search("count", v, 1, itemType);
                    Selected();
                    break;
            }
        }

        private void tscboSearch_SelectedIndexChanged(object sender, EventArgs e)
        {
            tslblOP.Text = tscboSearch.SelectedIndex.ToString();
            switch (Convert.ToInt32(tslblOP.Text))
            {
                case 0:
                    tslblOP.Text = "≌";
                    break;
                case 2:
                    tslblOP.Text = ">=";
                    break;
                default:
                    tslblOP.Text = "=";
                    break;
            }
        }
    }
}
